#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <purewell/purewell.hpp>
#include <purewell/semaphore.hpp>
using namespace purewell;

#include <iostream>
using namespace std;

const char semname[] = "/purewell.dat"; 

int
main(int argc, char* argv[])
{
	PWSemaphore* psem(PWSemaphore::create(1));
	if ( !psem )
	{
		fprintf(stderr, "%s", strerror(errno));
	}

	fork();

	cerr << "try lock: " << getpid() << endl;
	psem->lock();
	cerr << "locked: " << getpid() << endl;
	size_t v;
	psem->getValue(v);
	cerr << "value: " << v << endl;

	sleep(1);

	cerr << "unlock: " << getpid() << endl;
	psem->unlock();

	PWSemaphore::release(psem);

	PWSemaphore::unlink(semname);

	return 0;
}

